package valve_test.forms;

import valve_test.drawning.graph_pane;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;

/**
 * Created with IntelliJ IDEA.
 * User: Shinigami
 * Date: 15.06.14
 * Time: 20:55
 * To change this template use File | Settings | File Templates.
 */
public class graph_valves extends control{
    private JPanel graph_main_panel;
    private JButton exit_bt;
    private JScrollBar scrollBar1;
    private JScrollBar scrollBar2;
    private JFrame frame;

    public graph_valves() {
        frame = new JFrame("Графы");
        frame.getContentPane().add(graph_main_panel);
        frame.setSize(800, 600);
        frame.setVisible(true);
        scrollBar1.setValue(0);
        scrollBar2.setValue(0);
        scrollBar1.setMinimum(0);
        scrollBar2.setMinimum(0);
        exit_bt.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                frame.setVisible(false);
            }
        });
        scrollBar1.addAdjustmentListener(new AdjustmentListener() {
            @Override
            public void adjustmentValueChanged(AdjustmentEvent e) {
                graph_pane.setX_offset(scrollBar1.getValue());
                frame.repaint();
            }
        });

        scrollBar2.addAdjustmentListener(new AdjustmentListener() {
            @Override
            public void adjustmentValueChanged(AdjustmentEvent e) {
                graph_pane.setY_offset(scrollBar2.getValue());
                frame.repaint();
            }
        });
    }

    public void refresh(int max_x, int max_y){
        if (max_x == 0) {
            scrollBar1.setValue(0);
            graph_pane.setX_offset(0);
            scrollBar1.setMaximum(max_x);
            scrollBar1.setVisible(false);
        }
        else {
            if (scrollBar1.getMaximum() != max_x){
                scrollBar1.setVisible(true);
                scrollBar1.setMaximum(max_x);
                frame.repaint();
            }
        }
        if (max_y == 0) {
            scrollBar2.setValue(0);
            graph_pane.setY_offset(0);
            scrollBar2.setMaximum(max_y);
            scrollBar2.setVisible(false);
        }
        else {
            if (scrollBar2.getMaximum() != max_y){
                scrollBar2.setVisible(true);
                scrollBar2.setMaximum(max_y);
                frame.repaint();
            }
        }
    }

    public void Show(){
        frame.setVisible(true);
    }

}
